import json
from glob import glob

def write_output(glob_path, output_path):
    # files = list(glob_path.glob('*.json'))
    files = glob(glob_path)
    files.sort()
    with open(output_path, 'w') as outfile:
        output_data = {}
        for path in files:
            with open(path, 'r') as f:
                data = json.load(f)
                output_data.update(data)
            # path.unlink()
        json.dump(output_data, outfile, indent=4, ensure_ascii=False)
    # glob_path.rmdir()

glob_path = '/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/CWQ-t5-base/checkpoint/_CWQ_test_SPQA-gpu*.json'
write_path = '/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/CWQ-t5-base/checkpoint/CWQ_test_SPQA.json'
write_output(glob_path, write_path) 

glob_path = '/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/Merge-t5-base/checkpoint/_CWQ_test_SPQA-gpu*.json'
write_path = '/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/Merge-t5-base/checkpoint/Merge_CWQ_test_SPQA.json'
write_output(glob_path, write_path)

glob_path = '/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/Merge-t5-base/checkpoint/_WebQSP_test_SPQA-gpu*.json'
write_path = '/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/Merge-t5-base/checkpoint/Merge_WebQSP_test_SPQA.json'
write_output(glob_path, write_path)

"""
target:
scp xionggm@192.168.4.200:/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/CWQ-t5-base/checkpoint/CWQ_test_SPQA.json ./
scp xionggm@192.168.4.200:/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/Merge-t5-base/checkpoint/Merge_CWQ_test_SPQA.json ./
scp xionggm@192.168.4.200:/home/xionggm/codes/decode-answer-logical-form/DecAF/Reading/save-FiD/Merge-t5-base/checkpoint/Merge_WebQSP_test_SPQA.json ./
"""